﻿using SHDM.Web.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace SHDM.Web.DataAccess
{
    public partial class SHDMEntities
    {
        public List<DicReader> SqlQueryDicReader(string sql, params object[] parameters)
        {
            using (IDbCommand command = this.Database.Connection.CreateCommand())
            {
                if (this.Database.Connection.State != ConnectionState.Open)
                {
                    this.Database.Connection.Open();
                }
                command.CommandText = sql;
                command.CommandTimeout = command.Connection.ConnectionTimeout;
                if (parameters != null && parameters.Length > 0)
                {
                    foreach (var item in parameters)
                    {
                        command.Parameters.Add(item);
                    }
                }
                List<DicReader> list = new List<DicReader>();
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var dic = new DicReader();
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            dic.Add(reader.GetName(i), reader.GetValue(i));
                        }
                        list.Add(dic);
                    }
                    reader.Close();
                    this.Database.Connection.Close();
                    return list;
                }
            }
        }
    }
}